//JS容错代码，不提示错误。
window.onerror=function (){return true;}
var PhotoLocalName = "";
var imgType = 1;
jQuery(function ($)
{
	// /ASUploader.swf
    swfobject.embedSWF("/IMGASUploader.swf", "uploadroom", "60", "22", "10.0.0", "expressInstall.swf", 

    {
        url : "http://room.img.17guagua.com/uploadImg.jsp", txt : "\u8bf7\u9009\u62e9", p : "1", align : "left", callback : "setType(1)",maxcallback:"imgSizeAlert"
    },
    {
        wmode : "transparent"
    },
    {});
});

function delImg(srcImg)
{
    var index = srcImg.indexOf("?");
    if (index > 0) {
        srcImg = srcImg.substring(0, index);
    }
    var request = $.ajax(
    {
        url : "/room!delImg.jspa", type : "post", data : {
            imgsrc : srcImg
        },
        dataType : "json", complete : function (data, textStatus) { }
    });
}
function subRoomInfo()
{
    if (getlengthStr($('#description').val()) > 100) {
        alert("\u5b57\u6570\u9650\u5236\u4e3a0~50\u4e2a\u6c49\u5b57");
        return false;
    }
    
    $.ajax({
		type: "post",		
		url: "/room!editRoomInfo.jspa", 
		data: {
			"room.description":$('#description').val(),"room.create_time":$('#create_time').val(),
		 	"room.room_id":$('#room_id').val(),"photo.photo_path":$('#roomavatar').attr("src"),
		  	"photo.photo_type_id":1,"photo.size_id":1,"photo.photo_name":PhotoLocalName
		  },
        dataType : "json",
        complete : function (data, textStatus)
        {
            if (eval("(" + data.responseText + ")").result == 0)
            {   
			    if($('#create_time').val()==""){
			       $('#create_time').val("100");
			    }
                //$('#preroomavatar').attr("src", $('#roomavatar').attr("src"));
                alert("\u4FDD\u5B58\u6210\u529F");
            }
            else {
                alert("\u4FDD\u5B58\u5931\u8D25");
            }
        }
    });
}

function imgSizeAlert(){
   alert("\u6587\u4EF6\u6700\u5927\u4E3A500K,\u8D85\u51FA\u5141\u8BB8\u8303\u56F4\uFF0C\u8BF7\u91CD\u65B0\u9009\u62E9\u6587\u4EF6");
   return;
}
function setType(type)
{
    imgType = type;
}
//  \u521D\u59CB\u5316,as \u8C03\u7528\u521D\u59CB\u5316\u3002
function fileInit() { } //   0\u5931\u8D25 \u4E0D\u7B49\u4E8E0 \u5219\u56FE\u7247\u8DEF\u5F84
function cHandler(result, name)
{ 
    //var ret = result.replace(/(^\s*)|(\s*$)/g, "");
   if (eval("(" + result + ")").result == 0){
        
        PhotoLocalName = name;
        if (imgType != 3) {
            showCropDiv(eval("(" + result + ")").value, imgType);
        }
        else {
            showCropActiveDiv(eval("(" + result + ")").value);
        }
    }else if (eval("(" + result + ")").result == -2){
        alert("\u56FE\u7247\u5C3A\u5BF8\u4E0D\u6B63\u786E\uFF0C\u56FE\u7247\u8981\u6C42\u4ECB\u4E8E150*150 \u548C1024*1024\u4E4B\u95F4");
        return false;
    }else if (eval("(" + result + ")").result == -3){
        alert("\u56FE\u7247\u5C3A\u5BF8\u4E0D\u6B63\u786E\uFF0C\u56FE\u7247\u8981\u6C42\u4ECB\u4E8E511*262 \u548C1024*1024\u4E4B\u95F4");
        return false;
    }else if (eval("(" + result + ")").result == -4){
        alert("\u6b64\u56fe\u7247\u957f\u5bbd\u6bd4\u4f8b\u4e0d\u7b26\u5408 150*150 \u8303\u56f4\uff0c\u8bf7\u91cd\u65b0\u9009\u53d6\u3002");
        return false;
    }else{
        alert("\u4E0A\u4F20\u56FE\u7247\u5931\u8D25");
        return false;
    }
}
// \u8FDB\u5EA6\uFF0Ci \u8FDB\u5EA6\u503C\uFF0C\u57281-100\u76F4\u63A5
function progressHandler(i)
{
    $("#uploadprocess").show();
    $("#uploadBar").css("width", i * 2 + "px");
    if (i == 100) {
        $("#uploadprocess").hide();
    }
}
// \u8BBE\u7F6E\u53C2\u6570
function setKey(p)
{
    thisMovie("uploadstar").setKey(p);
}
function thisMovie(movieName) 
{
    //return swfobject.getObjectById(movieName);
    if (navigator.appName.indexOf("Microsoft") != - 1) {
        return window[movieName] 
    }
    else {
        return document[movieName] ;
    }
}
function computDate()
{
    $("#endtime").html("");
	if ( $("#startime_hour").val() !="" && Number($("#startime_hour").val())>=24){
		    $("#startime_hour").val("00");
		    alert("\u5F55\u5165\u65F6\u95F4\u4E0D\u6B63\u786E\uFF0C\u8BF7\u91CD\u65B0\u8F93\u5165");
         }	
	if ($("#startime_min").val() !="" && Number($("#startime_min").val())>=60){
			$("#startime_min").val("00");
		    alert("\u5F55\u5165\u65F6\u95F4\u4E0D\u6B63\u786E\uFF0C\u8BF7\u91CD\u65B0\u8F93\u5165");
         }
    if ($("#startime_str").val() != "\u5F00\u59CB\u65E5\u671F" && $("#startime_hour").val() !="" && $("#startime_min").val() !=""&&$("#active_time").val() >0)
    {	
        var startime = new Date(($("#startime_str").val() + " "+$("#startime_hour").val()+":"+$("#startime_min").val()+":00").replace(/-/g,  "/"));
        startime.setMinutes(startime.getMinutes() + Number($("#active_time").val()), startime.getSeconds(), 0);
		$("#endtime").html(startime.getFullYear()+"-"+("" + (startime.getMonth() + 101)).substr(1)+"-"+ ("" + (startime.getDate() + 100)).substr(1)+" "+("" + (startime.getHours() + 100)).substr(1)+":"+ ("" + (startime.getMinutes() + 100)).substr(1));
    }
}
function showCropDiv(imgUrl, picType)
{
    $('#target').attr("src", imgUrl);
    $('#preview').attr("src", imgUrl);
    initCrop();
    $('#cropImg').show();
    $('#subCrop').unbind();
    $('#subCrop').click(function ()
    {
        subCrop(picType);
    });
}
function subCrop(picType)
{
    var request = $.ajax(
    {
        url : "/room!cropImg.jspa", type : "post", data : 
        {
            x1 : $('#x1').val(), y1 : $('#y1').val(), x2 : $('#x2').val(), y2 : $('#y2').val(), mw : $('#hideDivForPre').width(), 
            mh : $('#hideDivForPre').height(), imgsrc : $('#target').attr("src")
        },
        dataType : "text",
        complete : function (data, textStatus)
        {   
           
            if(textStatus.indexOf("error") > 0 ){
               return;
            }
         try{
            if (eval("(" + data.responseText + ")").result == 0)
            {
                if (picType == 1)
                {
                    $('#roomavatar').attr("src", eval("(" + data.responseText + ")").value + "?c=" + (new Date().valueOf()));
                }
                else
                {
                    $('#avatar').attr("src", eval("(" + data.responseText + ")").value + "?c=" + (new Date().valueOf()));
                }
                jcrop_api.destroy();
                $('#cropImg').hide();
				$('#uploadactive').hide();
				$("#ywc_del_h").attr("href", "javascript:delImg($('#active_avatar').attr('src'));delActivePho(0);");
				$("#ywc").show();
				$("#ywc_pho_name").html(PhotoLocalName);
            }
            else {
                alert("\u88C1\u526A\u56FE\u7247\u5931\u8D25,\u7528\u6237\u672A\u767B\u5F55");
                return false;
            }
          }catch(err)
				{
				   return;
				}
        }
    });
}
/*image auto resize*/
function AutoResizeImage(maxWidth, maxHeight, objImg) 
{
    var img = new Image();
    img.src = objImg.attr("src");
    img.onload = function ()
    {
        var hRatio;
        var wRatio;
        var Ratio = 1;
        var w = img.width;
        var h = img.height;
        wRatio = maxWidth  / w;
        hRatio = maxHeight  / h;
        if (maxWidth == 0 && maxHeight == 0) {
            Ratio = 1;
        }
        else 
        {
            if (maxWidth == 0) {
                //
                if (hRatio < 1) {
                    Ratio = hRatio;
                }
            }
            else 
            {
                if (maxHeight == 0) {
                    if (wRatio < 1) {
                        Ratio = wRatio;
                    }
                }
                else {
                    if (wRatio < 1 || hRatio < 1) {
                        Ratio = (wRatio <= hRatio ? wRatio : hRatio);
                    }
                }
            }
        }
        if (Ratio < 1) {
            w = w * Ratio;
            h = h * Ratio;
        }
        objImg.css("height", Math.round(h));
        objImg.css("width", Math.round(w));
        
        //initCropActive(Ratio);
    }
}
var prox;
var proy;
var proxc;
var proyc;
function show(id)
{
    /*--\u6253\u5F00--*/
    clearInterval(prox);
    clearInterval(proy);
    clearInterval(proxc);
    clearInterval(proyc);
    var o = document.getElementById(id);
    o.style.display = "block";
    prox = setInterval(function ()
    {
        openx(o, 793)
    }, 10);
}
function openx(o, x)
{
    /*--\u6253\u5F00x--*/
    var cx = parseInt(o.style.width);
    if (cx < x) { }
    else {
        clearInterval(prox);
        proy = setInterval(function ()
        {
            openy(o, 276)
        }, 10);
    }
}
function openy(o, y)
{
    /*--\u6253\u5F00y--*/
    var cy = parseInt(o.style.height);
    if (cy < y) { }
    else {
        clearInterval(proy);
    }
}
function closeed(id)
{
    /*--\u5173\u95ED--*/
    clearInterval(prox);
    clearInterval(proy);
    clearInterval(proxc);
    clearInterval(proyc);
    var o = document.getElementById(id);
    if (o.style.display == "block") {
        proyc = setInterval(function ()
        {
            closey(o)
        }, 10);
    }
}
function closey(o)
{
    /*--\u6253\u5F00y--*/
    var cy = parseInt(o.style.height);
    if (cy > 0) {
        o.style.height = (cy - Math.ceil(cy / 5)) + "px";
    }
    else {
        clearInterval(proyc);
        proxc = setInterval(function ()
        {
            closex(o)
        }, 10);
    }
}
function closex(o)
{
    /*--\u6253\u5F00x--*/
    var cx = parseInt(o.style.width);
    if (cx > 0) {
        o.style.width = (cx - Math.ceil(cx / 5)) + "px";
    }
    else {
        clearInterval(proxc);
        o.style.display = "none";
    }
}

function getlengthStr(str){
     return str.replace(/[^\x00-\xff]/g,"**").length;
}
function checkMax(oTextArea,max){
       var length=getlengthStr(oTextArea.value);
       if( length> max){
                 oTextArea.value = oTextArea.value.substring(0,oTextArea.value.replace(/[^\x00-\xff]/g,"**").substring(0,max).replace(/\*\*/g,"1").length); 
                } 
}
 function isURL(str) {
       var strRegex = "^http://[A-Za-z0-9-_]+\\.[A-Za-z0-9-_%&\?\/.=]+$";
       var re = new RegExp(strRegex);
       if (re.test(str)) {
                return true;
            } else {
                 return false;
            }
}
